<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/cityList/ 城市列表获取
 * @apiName cityList
 * @apiGroup Community
 *
 * @apiParam {Number} city 城市id/获取省级城市为0(必填).
 * @apiParam {Number} level
 * -  1省/直辖市
 * -  2市
 * -  3区
 * -  4小区
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/communityList/ 城市小区列表获取
 * @apiName communityList
 * @apiGroup Community
 *
 * @apiParam {Number} cid 小区id(必填).
 * @apiParam {Number} level(必填)
 * -  5楼栋
 * -  6房号
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Community/authentication/ 注册户主
 * @apiName authentication
 * @apiGroup Community
 *
 * @apiParam {Number} uid 用户uid(必填).
 * @apiParam {String} idlist  1-2-3 用户选择的小区-楼栋-门牌号(必填).
 * @apiParam {String} name 用户名(必填).
 * @apiParam {Number} phone 用户电话号码(必填).
 * @apiParam {Number} usertype 用户类型(必填)
 * -  2业主
 * @apiParam {String} idcard 省份证号码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/examineList/ 获取用户房产认证列表
 * @apiName examineList
 * @apiGroup Community
 *
 * @apiParam {Number} uid 用户uid(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 * @apiSuccess {NUmber} status  0认证中1认证通过2冻结3删除.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/residentsList/ 获取该房产下住客/户主/租客列表
 * @apiName residentsList
 * @apiGroup Community
 *
 * @apiParam {Number} aid 用户aid为获取用户房产认证列表id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 * @apiSuccess {NUmber} status  0认证中1认证通过2冻结3删除.
 * @apiSuccess {NUmber} data的key  1家属 2户主 3租客.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/zukeFamilyList/ 获取租客家属列表
 * @apiName zukeFamilyList
 * @apiGroup Community
 *
 * @apiParam {Number} fid 用户fid为获取用户房产认证列表id(必填).
 * @apiParam {Number} user_id 用户user_id为获取用户房产认证列表id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Community/residentsadd/ 添加户主家属/租客
 * @apiName residentsadd
 * @apiGroup Community
 *
 * @apiParam {Number} fid 获取该房产下住客列表中id(必填).
 * @apiParam {String} name 用户名(非必填，存在自定义人脸时需要).
 * @apiParam {String} img 图片转码base64格式(非必填，存在自定义人脸时需要).
 * @apiParam {Number} userid 用户id(非必填，不存在自定义时存人脸时需要).
 * @apiParam {Number} usertype 用户类型(必填)
 * -  1家属
 * -  3租客
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Community/tenantFamilyAdd/ 注册租客家属
 * @apiName tenantFamilyAdd
 * @apiGroup Community
 *
 * @apiParam {Number} fid 获取该房产下住客列表中id(必填).
 * @apiParam {String} name 用户名(非必填，存在自定义人脸时需要).
 * @apiParam {String} img 图片转码base64格式(非必填，存在自定义人脸时需要).
 * @apiParam {Number} userid 用户id(非必填，不存在自定义时存人脸时需要).
 * @apiParam {Number} family_id 用户家属id(非必填，不存在自定义时存人脸时需要).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/searchUser/ 搜索访客用户列表
 * @apiName searchUser
 * @apiGroup Community
 *
 * @apiParam {String} condition 搜索条件 用户名/手机号码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteUser/ 访客邀请
 * @apiName inviteUser
 * @apiGroup Community
 *
 * @apiParam {Number} user_id 被邀请人id(存在人脸为不填必填).
 * @apiParam {Number} fid 邀请户主(必填).
 * @apiParam {Number} loc_uid 邀请人id(必填).
 * @apiParam {Number} startime 开始时间(必填).
 * @apiParam {Number} endtime 结束时间(必填).
 * @apiParam {String} name 用户名(非必填，存在自定义人脸时需要).
 * @apiParam {String} img 图片转码base64格式(非必填，存在自定义人脸时需要).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteshow/ 获取邀请页面数据
 * @apiName inviteshow
 * @apiGroup Community
 *
 * @apiParam {Number} id 邀请id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteupdate/ 更改邀请状态
 * @apiName inviteupdate
 * @apiGroup Community
 *
 * @apiParam {Number} id 邀请id(必填).
 * @apiParam {Number} status 1接收 2拒绝(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/searchspace/ 搜索小区
 * @apiName searchspace
 * @apiGroup Community
 *
 * @apiParam {String} keyword 小区搜索关键字(必填).
 * @apiParam {Number} limit   页码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/invitelist/ 被邀请人列表
 * @apiName invitelist
 * @apiGroup Community
 *
 * @apiParam {Number} uid   用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/residentsdel/ 删除住户
 * @apiName residentsdel
 * @apiGroup Community
 *
 * @apiParam {Number} did   批量删除各个id通过“,”串联住户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/invitedel/ 删除邀请
 * @apiName invitedel
 * @apiGroup Community
 *
 * @apiParam {Number} did   批量删除各个id通过“,”串联 邀请记录id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/authList/ 我的所有房子的列表（合并为小区）
 * @apiName authList
 * @apiGroup Community
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} type 当type为1则显示全部包括房子的列表(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use app\common\controller\Api;
use think\Cache;
use \think\Log;
use think\Db;
use think\Model;
use think\Session;

class Community extends Api{
    //查找城市，小区等
    public function cityList(){
        //获取省市级 city 城市id 0为省级城市 level 为级别1为省级
        $result = $this->request->param();
        if ($result['city'] == 0 && $result['level'] == 1){
            $where['level'] = 1;
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 2){
            $where['level'] = 2;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 3){
            $where['level'] = 3;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] = 4){
            //获取小区
            $where['level'] = 4;
            $where['community_id'] = $result['city'];
            $data = M('Community')->where($where)->select();
        }else{
            $data = '';
        }
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
        }else{
            $return = codemsg(-7000);
        }
        echo json_encode($return);
    }

    //获取小区列表
    public function communityList(){
        $result = $this->request->param();
        if ($result['cid'] > 0 && $result['level'] == 5){
            //获取小区的楼栋
            $where['parent_id'] = $result['cid'];
            $where['level'] = $result['level'];
            $data = M('community')->where($where)->select();
        }elseif ($result['cid'] > 0 && $result['level'] == 6){
	   $data = array();
            //获取小区的门户
            $where['c.parent_id'] = $result['cid'];
            $where['c.level'] = $result['level'];
            $dataa = M('community')
                ->alias('c')
                ->field('c.id,c.name,c.level,c.parent_id,c.community_id,c.status,a.uid,a.id as fid')
                ->join('auths a','c.uid=a.id','left')
                ->where($where)
                ->select();
            if ($dataa){
            
                foreach ($dataa as $k=>$v){
                    $data[$k] = $v;
                    if ($v['uid']==null){
                        $data[$k]['uid'] = 0;
                    }
                    if ($v['fid']==null){
                        $data[$k]['fid'] = 0;
                    }
                }
            }
        }else{
            $data = '';
        }
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
        }else{
            $return = codemsg(-7000);
        }

        echo json_encode($return);
    }

    //户主认证 uid(用户人脸存在不需要人脸照片) idlist 89-89-52 name phone usertype idcard
    public function authentication(){
//        if(!empty($_POST)){
        $result = $this->request->param();
//            $result['uid'] = 13;
//            $result['idlist'] = "1-2-3";
//            $result['name'] = '张建';
//            $result['phone'] = 15827024197;
//            $result['usertype'] = 2;
//            $result['idcard'] = '340826199410232258';
//            $face = new Face();
//            $result['img'] = $face->base64('http://imgbdb3.bendibao.com/shanghai/20152/4/201524103623754.jpg');
        Log::api('【Community/authentication】-[参数]--'.json_encode($result));
        //格式 55-88-44
        if (empty($result['idlist'])){
            //不存在该参数
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $id = explode('-',$result['idlist']);
        //小区id，查询是否存在小区
        if ($id[0] >0){
            $xwhere['id'] = $id[0];
            $xwhere['level'] = 4;
            $findcom = M('community')->where($xwhere)->find();
            if (empty($findcom)){
                //不存在该小区
                $return = codemsg(-7002);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //小区id错误
            $return = codemsg(-7002);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //楼栋id
        if ($id[1] >0){
            $lwhere['id'] = $id[1];
            $lwhere['level'] = 5;
            $findcoml = M('community')->where($lwhere)->find();
            if (empty($findcoml)){
                //不存在该楼栋
                $return = codemsg(-7003);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //楼栋id错误
            $return = codemsg(-7003);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //房号id
        if ($id[2] >0){
            $lwhere['id'] = $id[2];
            $lwhere['level'] = 6;
            $findcomf = M('community')->where($lwhere)->find();
            if (empty($findcomf)){
                //不存在该房号
                $return = codemsg(-7004);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //房号id错误
            $return = codemsg(-7004);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //姓名
        if (empty($result['name'])){
            //不能为空
            $return = codemsg(-7005);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //手机号码
        $phone = regcheck('phone',$result['phone']);
        if ($phone){
            //电话号码错误
            Log::api('【Community/authentication】-[返回]--'.json_encode($phone));
            echo json_encode($phone);die;
        }
        //身份证号
        $idcart = regcheck('idcard',$result['idcard']);
        if ($idcart){
            //身份证号码错误
            Log::api('【Community/authentication】-[返回]--'.json_encode($idcart));
            echo json_encode($idcart);die;
        }
        $usertype = array(
            1=>'家属',
            2=>'业主',
            3=>'租客',
            4=>'访客'
        );
        //身份
        if (empty($usertype[$result['usertype']])){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['uid'] <=0 ){
            //用户信息错误
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //判断用户身份存在人脸
        if ($result['img']){
            $fwhere['uid'] = $result['uid'];
            $fwhere['phone'] = $result['phone'];
            $fwhere['status'] = 1;
            $findFace = M('face_openid')->where($fwhere)->find();
            if (empty($findFace)){
                //没有脸部信息存信息
                $face = new Face();
                $facedata['img'] = $result['img'];
                $facedata['userid'] = $result['uid'];
                $facedata['phone'] = $result['phone'];
                $addface = $face->Face('add',$facedata);
                if ($addface <= 0){
                    //新增失败
                    $return = codemsg(-7007);
                    Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                    echo json_encode($return);die;
                }
            }
        }
        //获取省市区地址
        $qid['id'] = array('eq',$findcom['community_id']);
        $q = M('region')->where($qid)->find();
        //市
        $sid['id'] = array('eq',$q['parent_id']);
        $s = M('region')->where($sid)->find();
        //省
        $slid['id'] = array('eq',$s['parent_id']);
        $sl = M('region')->where($slid)->find();
        Db::startTrans();
        try{
            //存用户信息
            $dataadd['username'] = $result['name'];
            $dataadd['phone'] = $result['phone'];
            $dataadd['card'] = $result['idcard'];
            $dataadd['uid'] = $result['uid'];
            //小区名
            $dataadd['xiaoqu'] = $findcom['name'];
            $dataadd['dz'] = $sl['name']." ".$s['name']." ".$q['name'];
            $dataadd['lou'] = $findcoml['name'].$findcomf['name'];
            $dataadd['pic'] = '/public/upload/pic/default.jpg';
            //前台添加
            $dataadd['type'] = 1;
            //看中的房子id
            $dataadd['fid'] = $id[2];
            //查询是否存在该审核记录
            $findfangzi['phone'] = array('eq',$result['phone']);
            $findfangzi['card'] = array('eq',$result['idcard']);
            $findfangzi['status'] = array('neq','1,2');
            $findfangzi['uid'] = array('eq',0);
            $findfangzi['type'] = array('eq',0);

            $is_fangzi = M('auths')->where($findfangzi)->find();
            //后台已经导入用户信息
            if ($is_fangzi){
                $savewhere['id'] = array('eq',$is_fangzi['id']);
                $savemap['uid'] = $result['uid'];
                $savemap['status'] = 1;
                $svae_fangzi = M('auths')->where($savewhere)->update($savemap);

                $swhere['id'] = $id[2];
                $swhere['level'] = 6;
                $map['uid'] = $is_fangzi['id'];
                $map['status'] = 1;
                $add_togg = M('community')->where($swhere)->update($map);
            }else{
                $add = M('auths')->insertGetId($dataadd);
                if ($add <=0){
                    $return = codemsg(-7007);
                    Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                    echo json_encode($return);die;
                }
//                $swhere['id'] = $id[2];
//                $swhere['level'] = 6;
//                $map['uid'] = $add;
//                $save = M('community')->where($swhere)->update($map);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            //记录错误日志
            Db::rollback();
            Log::api('【Community/authentication】-[返回]--'.json_encode($e));
            //返回错误
            $return = codemsg(-7007);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询用户的人脸是否存在
        $facewhere['uid'] = array('eq',$result['uid']);
        $facewhere['usertype'] = array('eq',2);
        $facewhere['status'] = array('eq',1);
        $is_face = M('face_openid')->where($facewhere)->find();
        if ($is_face){
            $face = 1;
        }else{
            $face = -1;
        }
        if ($add){
            $return = codemsg(5009);
            $return['data'] = array(
                'is_face'=>$face,
                'is_auth'=>-1
            );
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }elseif ($add_togg){
            $return = codemsg(5008);
            $return['data'] = array(
                'is_face'=>$face,
                'is_auth'=>1
            );
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
//        }else{
//            //不是post提交
//            $return = codemsg(-7);
//            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
//            echo json_encode($return);die;
//        }
    }

    //审核列表
    public function examineList(){
        $result = $this->request->param();
//        $result['uid'] = 13;
        //获取uid
        if ($result['uid'] <=0 || empty($result['uid'])){
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该用户下的房子
        $where['a.uid'] = array('eq',$result['uid']);
        $where['a.status'] = array('in',array(0,1,2));
        $datalist = M('auths')
            ->alias('a')
            ->field('a.*,c.id as cid')
            ->join('tp_community c','c.id=a.fid')
            ->where($where)
            ->select();
        if ($datalist){
            $return = codemsg(5003);
            $return['data'] = $datalist;
            Log::api('【Community/examineList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7022);
            Log::api('【Community/examineList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //户主/家人/租客列表
    public function residentsList(){
        $result = $this->request->param();
//            $result['aid'] = 21;
        //获取查看户主表id是否正确
        if ($result['aid'] <=0 || empty($result['aid'])){
            $return = codemsg(-1);
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取户主信息
        $where['id'] = array('eq',$result['aid']);
        $datalist = M('auths')
            ->where($where)
            ->find();
        if ($datalist){
            $datalist['rname'] = $datalist['username'];
            $rlist[2] = $datalist;
        }else{
            $rlist[2] = array();
        }

        $rlist[1] = array();
        $rlist[3] = array();
        //获取租客/家属等信息
        $fwhere['fid'] = array('eq',$result['aid']);
        $fwhere['is_del'] = array('eq',0);
        $rdata = M('residents')
                ->field('id,rname,uid,faceopen,fid,status')
                ->where($fwhere)
                ->select();
        if ($rdata){
            //取出uid
            $uidlist = implode(',',array_filter(array_column($rdata,'uid')));
            //搜索用户
            if ($uidlist){
                $userwhere['user_id'] = array('in',$uidlist);
                $userwhere['is_lock'] = array('eq',0);
                $userlist = M('users')->where($userwhere)->column('user_id,nickname,head_pic');
            }
            foreach ($rdata as $k=>$v){
                if (!empty($v['uid'])){
                    if ($v['status']==1){
                        $rlist[1][$k] = $v;
                        $rlist[1][$k]['rname'] = $userlist[$v['uid']]['nickname'];
                        $rlist[1][$k]['pic'] = $userlist[$v['uid']]['head_pic'];
                    }else{
                        $rlist[3][$k] = $v;
                        $rlist[3][$k]['rname'] = $userlist[$v['uid']]['nickname'];
                        $rlist[3][$k]['pic'] = $userlist[$v['uid']]['head_pic'];
                    }
                }else{
                    if ($v['status']==1){
                        $rlist[1][$k] = $v;
                        $rlist[1][$k]['pic'] = '/upload/face/'.$v['faceopen'].'.png';
                    }else{
                        $rlist[3][$k] = $v;
                        $rlist[3][$k]['pic'] = '/upload/face/'.$v['faceopen'].'.png';
                    }
                }
            }
        }
        $a = array();
        if ($rlist[3]){
            foreach ($rlist[3] as $kk=>$vv){
                $a[] = $vv;
            }
        }else{
            $a = array();
        }
        if ($rlist[1]){
            foreach ($rlist[1] as $kkk=>$vvv){
                $b[] = $vvv;
            }
        }else{
            $b = array();
        }

        $rlist[3] = $a;
        $rlist[1] = $b;
//        dump($rlist);die;
        if ($rlist){
            $return = codemsg(5003);
            $return['data'] = $rlist;
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //租客家属列表
    public function zukeFamilyList(){
        $result = $this->request->param();
        //判断是否存在存在该租客
        if ($result['user_id'] <= 0 || empty($result['user_id']) || $result['fid'] <= 0 || empty($result['fid'])){
            $return = codemsg(-1);
            Log::api('【Community/zukeFamilyList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //判断是否存在该用户
        $where['user_id'] = array('eq', $result['user_id']);
        $where['is_lock'] = array('eq', 0);
        $user = M('users')->where($where)->find();
        if (empty($user)){
            $return = codemsg(-4003);
            Log::api('【Community/zukeFamilyList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询用户信息
        $zkwhere['fid'] = array('eq',$result['fid']);
        $zkwhere['uid'] = array('eq',$result['user_id']);
        $zkwhere['status'] = array('eq',3);
        $zkwhere['is_del'] = array('eq',0);
        $datalist['zuke'] = M('residents')->where($zkwhere)->find();
        $datalist['family'] = array();
        if ($datalist['zuke']){
            $familywhere['fid'] = array('eq',$result['fid']);
            $familywhere['is_del'] = array('eq',0);
            $familywhere['loc_uid'] = array('eq',$result['user_id']);
            $datalist['family'] = M('residents')->where($familywhere)->select();
        }
        if ($datalist){
            $return = codemsg(5003);
            $return['data'] = $datalist;
            Log::api('【Community/zukeFamilyList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【Community/zukeFamilyList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //添加住户/租客
    public function residentsadd(){
        $result = $this->request->param();
//        $result['fid'] = 21;
//        $result['name'] = '李四';
//        $result['usertype'] = 1;
//        $face = new Face();
//        $result['img'] = $face->base64('http://www.js.xinhuanet.com/2016-04/17/1118645391_14608724772851n.jpg');
        Log::api('【Community/residentsadd】-[参数]--'.json_encode($result));
        //判断fid是否存在
        if ($result['fid'] <= 0 && empty($result['fid'])){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $findwhere['id'] = $result['fid'];
        $findwhere['status'] = 1;
        $findauth = M('auths')->where($findwhere)->find();
        if (empty($findauth)){
            $return = codemsg(-7008);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //身份
        if ($result['usertype'] != 1 && $result['usertype'] != 3) {
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($result['userid'] <=0 || empty($result['userid'])) {
            //自定义添加
            //姓名
            if (empty($result['name'])) {
                //不能为空
                $return = codemsg(-7005);
                Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            //判断用户身份存在人脸
            $fwhere['uid'] = $findauth['uid'];
            //通过uid获取到户主电话
            $huzhuwhere['user_id'] = array('eq', $fwhere['uid']);
            $huzhuwhere['is_lock'] = array('eq', 0);
            $huzhuphone = M('users')->where($huzhuwhere)->find();
            if (empty($huzhuphone)) {
                $return = codemsg(-7008);
                Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $face = new Face();
            $facedata['img'] = $result['img'];
            $facedata['userid'] = $findauth['uid'];
            $facedata['phone'] = $huzhuphone['mobile'];
            $facedata['usertype'] = $result['usertype'];
            $facedata['flag'] = 1;
            $addface = $face->Face('add', $facedata);
            if ($addface['code'] <= 0) {
                //新增失败
                $return = codemsg(-7007);
                Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            //存储家人/租客信息
            $map['pic'] = '/upblic/upload/pic/default.jpg';
            $map['rname'] = $result['name'];
            $map['faceopen'] = $addface['token'];
            $map['fid'] = $result['fid'];
            $map['status'] = $result['usertype'];
            $inset = M('residents')->insert($map);
        }else{
            //已存在用户添加
            //查找该用户
            $userwhere['user_id'] = array('eq',$result['userid']);
            $userwhere['is_lock'] = array('eq', 0);
            $user = M('users')->where($userwhere)->find();
            if (empty($user)) {
                $return = codemsg(-7011);
                Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $map['pic'] = $user['head_pic'];
            $map['phone'] = $user['mobile'];
            $map['rname'] = $user['nickname'];
            $map['fid'] = $result['fid'];
            $map['uid'] = $user['user_id'];
            $map['status'] = $result['usertype'];
            $inset = M('residents')->insert($map);
        }
        if($inset>0){
            $return = codemsg(2);
            Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //添加租客家属 fid 房子的id 租客id img
    public function tenantFamilyAdd(){
        $result = $this->request->param();
//        $result['fid'] = 21;
//        $result['name'] = '李四';
//        $face = new Face();
//        $result['img'] = $face->base64('http://www.js.xinhuanet.com/2016-04/17/1118645391_14608724772851n.jpg');
        Log::api('【Community/tenantFamilyAdd】-[参数]--'.json_encode($result));
        $usertype = 3;
        //判断fid是否存在
        if ($result['fid'] <= 0 && empty($result['fid'])){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/tenantFamilyAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $findwhere['id'] = $result['fid'];
        $findwhere['status'] = 1;
        $findauth = M('auths')->where($findwhere)->find();
        if (empty($findauth)){
            $return = codemsg(-7008);
            Log::api('【Community/tenantFamilyAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询租客是否存在或未被冻结
        $zukewhere['user_id'] = array('eq',$result['userid']);
        $zukewhere['is_lock'] = array('eq', 0);
        $huzhuphone = M('users')->where($zukewhere)->find();
        if (empty($huzhuphone)){
            $return = codemsg(-7041);
            Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //查询该租客是否在该房子下面
        $ahouwhere['uid'] = array('eq',$result['userid']);
        $ahouwhere['fid'] = array('eq',$result['fid']);
        $ahouwhere['is_del'] = array('eq',0);
        $ahouwhere['status'] = array('eq',3);
        $ahoufind = M('residents')->where($ahouwhere)->find();
        if (empty($ahoufind)){
            $return = codemsg(-7041);
            Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($result['family_id'] <=0 || empty($result['family_id'])) {
            //自定义添加
            //姓名
            if (empty($result['name'])) {
                //不能为空
                $return = codemsg(-7005);
                Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $fwhere['uid'] = $findauth['uid'];
            //通过uid获取到户主电话
            $huzhuwhere['user_id'] = array('eq', $fwhere['uid']);
            $huzhuwhere['is_lock'] = array('eq', 0);
            $huzhuphone = M('users')->where($huzhuwhere)->find();
            if (empty($huzhuphone)) {
                $return = codemsg(-7008);
                Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $face = new Face();
            $facedata['img'] = $result['img'];
            $facedata['userid'] = $findauth['uid'];
            $facedata['phone'] = $huzhuphone['mobile'];
            $facedata['usertype'] = $usertype;
            $facedata['flag'] = 1;
            $addface = $face->Face('add', $facedata);
            if ($addface['code'] <= 0) {
                //新增失败
                $return = codemsg(-7007);
                Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            //存储家人/租客信息
            $map['pic'] = '/upload/pic/default.jpg';
            $map['rname'] = $result['name'];
            $map['faceopen'] = $addface['token'];
            $map['fid'] = $result['fid'];
            $map['status'] = $usertype;
            $map['loc_uid'] = $result['userid'];
            $inset = M('residents')->insert($map);
        }else{
            //已存在用户添加
            //查找该用户
            $userwhere['user_id'] = array('eq',$result['family_id']);
            $userwhere['is_lock'] = array('eq', 0);
            $user = M('users')->where($userwhere)->find();
            if (empty($user)) {
                $return = codemsg(-7011);
                Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $map['pic'] = $user['head_pic'];
            $map['phone'] = $user['mobile'];
            $map['rname'] = $user['nickname'];
            $map['fid'] = $result['fid'];
            $map['uid'] = $user['family_id'];
            $map['status'] = $usertype;
            $map['loc_uid'] = $result['userid'];
            $inset = M('residents')->insert($map);
        }
        if($inset>0){
            $return = codemsg(2);
            Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/tenantFamilyAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //住户删除
    public function residentsdel(){
        $result = $this->request->param();
        Log::api('【Community/residentsdel】-[参数]--'.json_encode($result));
        if (empty($result['did'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $where['id'] = array('in',$result['did']);
        $map['is_del'] = 1;
        $data = M('residents')->where($where)->update($map);
        if ($data){
            $return = codemsg(3);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7017);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //我的所有房子的列表（合并为小区） type 1为全部显示 带有status 1为1的为家属 3为租客 其他为户主
    public function authList(){
        $user_id = I('post.user_id');
        $type = I('post.type');
        if ($user_id <=0 || empty($user_id)){
            $return = codemsg(-1);
            Log::api('【Community/authList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索为户主的列表
        //查询该用户下的房子
        $where['a.uid'] = array('eq',$user_id);
        $where['a.status'] = array('eq',1);
        $datalist = M('auths')
            ->alias('a')
            ->field('a.id,a.uid,a.xiaoqu,a.lou,a.dz')
            ->join('tp_community c','c.uid=a.id')
            ->where($where)
            ->select();
        $wherezk['r.is_del'] = array('eq',0);
        $wherezk['a.status'] = array('eq',1);

        $zukeList = M('residents')
                ->alias('r')
                ->field('a.id,a.uid,a.xiaoqu,a.lou,a.dz,r.status')
                ->join('tp_auths a','a.id=r.fid')
                ->where($wherezk)
                ->select();
        $list = array_merge($datalist,$zukeList);
        //去重
        if ($type==1){
            $datalist = $list;
        }else{
            $datalist = assoc_unique($list, 'xiaoqu');
        }
        if ($datalist){
            $return = codemsg(5003);
            $return['data'] = $datalist;
            Log::api('【Community/authList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【Community/authList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //访客邀请 user_id 被邀请人  fid 房子 邀请的户主 loc_uid
    public function inviteUser(){
        $user_id = I('post.user_id');
        $fid = I('post.fid');
        $loc_uid = I('post.loc_uid');
        $startime = strtotime(I('post.startime'));
        $endtime = strtotime(I('post.endtime'));
        $img = I('post.img');
        $name = I('post.name');


//        $fid = 21;
//        $name = '李四';
//        $loc_uid = 13;
//        $face = new Face();
//        $startime = strtotime('2018-09-27');
//        $endtime = strtotime('2018-09-29');
//        $img = $face->base64('http://p4.so.qhmsg.com/t01aa3c07662d9a65b9.jpg');


        if ($img){
            //存在以下
            if ($endtime){
                $endtime = $endtime+86399;
            }
            Log::api('【Community/inviteUser】-[参数]--'.json_encode('img-'.$img.'fid-'.$fid.'loc_uid-'.$loc_uid.'startime-'.$startime.'endtime-'.$endtime));
            //搜索邀请是否已经存在

            //查询是否存在邀请人用户
            $bwhere['user_id'] = array('eq',$loc_uid);
            $bwhere['is_lock'] = array('eq',0);
            $is_loc_user = M('users')->where($bwhere)->find();
            if(!$is_loc_user){
                $return = codemsg(-7011);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
            //查询该房子是否在该用户名下
            $authwhere['id'] = array('eq',$fid);
            $authwhere['status'] = array('eq',1);
            $auth = M('auths')->where($authwhere)->find();
            if (!$auth){
                //如果不为户主为住户
                if ($auth['uid'] != $loc_uid){
                    $reswhere['uid'] = array('eq',$loc_uid);
                    $reswhere['fid'] = array('eq',$fid);
                    $reswhere['is_del'] = array('eq',0);
                    $is_residents = M('residents')->where($reswhere)->find();
                    if (!$is_residents){
                        $return = codemsg(-7042);
                        Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                        echo json_encode($return);die;
                    }
                }
            }
            //存人脸相片
            $face = new Face();
            $facedata['img'] = $img;
            $facedata['userid'] = $loc_uid;
            $facedata['phone'] = $is_loc_user['mobile'];
            $facedata['usertype'] = 4;
            $facedata['flag'] = 1;
            $addface = $face->Face('add', $facedata);
            if ($addface['code'] <= 0) {
                //新增失败
                $return = codemsg(-7007);
                Log::api('【Community/residentsadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            //插入数据
            $map['aid'] = $fid;
            $map['loc_uid'] = $loc_uid;
            $map['phone'] = $is_loc_user['mobile'];
            $map['pic'] = '/upload/face/'.$addface['token'].'.png';
            $map['name'] = $name;
            $map['address'] = $auth['dz']." ".$auth['xiaoqu'];
            $map['invitation_id'] = $loc_uid;
            $map['aname'] = $is_loc_user['nickname'];
            $map['faceopen'] = $addface['token'];
            $map['createtime'] = time();
            if (empty($startime) && empty($endtime)){
                $map['long'] = 1;
            }elseif (!empty($startime) && empty($endtime)){
                $map['startime'] = $startime;
            }elseif (empty($startime) && !empty($endtime)){
                $map['endtime'] = $endtime;
            }else{
                $map['startime'] = $startime;
                $map['endtime'] = $endtime;
            }
            $add = M('visitors')->insertGetId($map);
            if ($add){
                //添加邀请
                $return = codemsg(3);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }else{
                $return = codemsg(-7007);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //存在以下
            if ($endtime){
                $endtime = $endtime+86399;
            }
            Log::api('【Community/inviteUser】-[参数]--'.json_encode('user_id-'.$user_id.'fid-'.$fid.'loc_uid-'.$loc_uid.'startime-'.$startime.'endtime-'.$endtime));
            //搜索邀请是否已经存在
            $where['uid'] = array('eq',$user_id);
            $where['aid'] = array('eq',$fid);
            $where['loc_uid'] = array('eq',$loc_uid);
            $where['is_del'] = array('eq',0);
            $data = M('visitors')->where($where)->select();
            //时间判断
            //当开始与结束时间都没有
            if ($data){
                if (empty($startime) && empty($endtime)){
                    foreach ($data as $k=>$v){
                        if ($v['long']==1){
                            $return = codemsg(-7013);
                            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                            echo json_encode($return);die;
                        }
                    }
                }elseif (!empty($startime) && empty($endtime)){
                    foreach ($data as $k=>$v){
                        if (strtotime($v['endtime']) > strtotime($startime)){
                            $return = codemsg(-7013);
                            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                            echo json_encode($return);die;
                        }
                    }
                }elseif (empty($startime) && !empty($endtime)){
                    foreach ($data as $k=>$v){
                        if (strtotime($v['startime']) < strtotime($endtime)){
                            $return = codemsg(-7013);
                            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                            echo json_encode($return);die;
                        }
                    }
                }else{

                }
            }
            //查询是否存在该被邀请用户
            $bwhere['user_id'] = array('eq',$user_id);
            $bwhere['is_lock'] = array('eq',0);
            $is_buser = M('users')->where($bwhere)->find();
            if(!$is_buser){
                $return = codemsg(-7011);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
            //查询是否存在邀请人用户
            $bwhere['user_id'] = array('eq',$loc_uid);
            $bwhere['is_lock'] = array('eq',0);
            $is_loc_user = M('users')->where($bwhere)->find();
            if(!$is_loc_user){
                $return = codemsg(-7011);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
            //查询该房子是否在该用户名下
            $authwhere['id'] = array('eq',$fid);
            $authwhere['status'] = array('eq',1);
            $auth = M('auths')->where($authwhere)->find();
            if (!$auth){
                $return = codemsg(-7042);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
            //如果不为户主为住户
            if ($auth['uid'] != $loc_uid){
                $reswhere['uid'] = array('eq',$loc_uid);
                $reswhere['fid'] = array('eq',$fid);
                $reswhere['is_del'] = array('eq',0);
                $is_residents = M('residents')->where($reswhere)->find();
                if (!$is_residents){
                    $return = codemsg(-7042);
                    Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                    echo json_encode($return);die;
                }
            }
            //插入数据
            $map['uid'] = $user_id;
            $map['aid'] = $fid;
            $map['loc_uid'] = $loc_uid;
            $map['phone'] = $is_loc_user['mobile'];
            $map['pic'] = $is_buser['head_pic'];
            $map['name'] = $is_buser['nickname'];
            $map['address'] = $auth['dz']." ".$auth['xiaoqu'];
            $map['invitation_id'] = $loc_uid;
            $map['createtime'] = time();
            $map['aname'] = $is_loc_user['nickname'];

            if (empty($startime) && empty($endtime)){
                $map['long'] = 1;
            }elseif (!empty($startime) && empty($endtime)){
                $map['startime'] = $startime;
            }elseif (empty($startime) && !empty($endtime)){
                $map['endtime'] = $endtime;
            }else{
                $map['startime'] = $startime;
                $map['endtime'] = $endtime;
            }
            $add = M('visitors')->insertGetId($map);
            if ($map){
                //邀请成功后推送
                $wherejg['uid'] = $user_id;
                $jg = M('jgid')->where($wherejg)->find();
                $jgid = $jg['ppid'];
                if ($is_buser['head_pic']){
                    $jushdata['pic'] = $is_buser['head_pic'];
                }else{
                    $jushdata['pic'] = '/public/upload/pic/default.jpg';
                }
                $jushdata['xiaoqu'] = $map['address'];
                $jushdata['username'] = $map['name'];
                $jushdata['invite_id'] = $add;
                $jushdata['title'] = '尊敬的智慧小区用户，您有一条邀请记录！';
                $jushdata['type'] = 1;//邀请
                sendjg($jushdata,$jgid);
                //添加邀请
                $return = codemsg(3);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }else{
                $return = codemsg(-7007);
                Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }
    }

    //访客记录删除
    public function invitedel(){
        $result = $this->request->param();
        Log::api('【Community/invitedel】-[参数]--'.json_encode($result));
        if (empty($result['did'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/invitedel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $where['id'] = array('in',$result['did']);
        $map['is_del'] = 1;
        $data = M('visitors')->where($where)->update($map);
        if ($data){
            $return = codemsg(3);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7017);
            Log::api('【Community/invitedel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

    }

    //访客搜索
    public function searchUser(){
        $result = $this->request->param();
        Log::api('【Community/searchUser】-[参数]--'.json_encode($result));
        if (empty($result['condition'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索用户
        $where['is_lock'] = array('eq',0);
        $where['mobile|nickname'] = array('like','%'.$result['condition'].'%');
        $userlist = M('users')->field('mobile,nickname,user_id,head_pic')->where($where)->select();
        if ($userlist){
            $return = codemsg(5003);
            $return['data'] = $userlist;
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7010);
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //小区搜索
    public function searchspace(){
        $result = $this->request->param();
        if (empty($result['keyword'])){
            $return = codemsg(-7000);
            Log::api('【Community/searchspace】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['limit'] <=0 || empty($result['limit'])){
            $result['limit'] = 1;
        }
        $star = ($result['limit']-1)*20;
        $end = $result['limit']*20;
        //搜索区级地址
        $citywhere['level'] = array('eq',3);
        $gitcity = Cache::get('searchspace_city_1');
        if (empty($gitcity)){
            $city = M('Region')->where($citywhere)->column('name','id');
            $gitcity = Cache::set('searchspace_city_1',$city,100000);
        }
        $where['name'] = array('like','%'.$result['keyword'].'%');
        $where['level'] = array('eq',4);
        $xioqu = M('community')->where($where)->limit($star,$end)->select();
        $data = array();
        if ($xioqu){
            foreach ($xioqu as $k=>$v){
                $data[$k] = $v;
                $data[$k]['city'] = $gitcity[$v['community_id']];
            }
        }
        $return = codemsg(5003);
        $return['data'] = $data;
        Log::api('【Community/searchspace】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //获取邀请页面数据
    public function inviteshow(){
        $result = $this->request->param();
        if (empty($result['id']) || $result['id'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该记录
        $where['id'] = $result['id'];
        $find = M('visitors')->where($where)->find();
        if ($find['status'] == 1){
            $return = codemsg(-7014);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($find['status'] == 2){
            $return = codemsg(-7015);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $return = codemsg(3);
        $return['data'] = $find;
        Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //邀请记录列表
    public function invitelist(){
        $result = $this->request->param();
        if (empty($result['uid']) ||$result['uid'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/invitelist】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取列表
        $where['loc_uid'] = $result['uid'];
        $datalist = M('visitors')
            ->field('id as iid,uid,phone,pic,name,aid,address,status,loc_uid,aname,time,startime,endtime')
            ->where($where)
            ->select();
        $return = codemsg(3);
        $return['data'] = $datalist;
        Log::api('【Community/invitelist】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //更改邀请状态
    public function inviteupdate(){
        $result = $this->request->param();
        Log::api('【Community/inviteupdate】-[参数]--'.json_encode($result));
        if (empty($result['id']) || $result['id'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['status'] !=1 && $result['status'] !=2){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该记录
        $where['id'] = $result['id'];
        $find = M('visitors')->where($where)->find();
        if ($find['status'] == 1){
            $return = codemsg(-7014);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($find['status'] == 2){
            $return = codemsg(-7015);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

        //更改状态
        $map['status'] = $result['status'];
        $save = M('visitors')->where($where)->update($map);
        if ($save){
            $return = codemsg(3);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
}